/*
 * Copyright (C) 2011  John Lawrence thegordo.john at gmail.com
 * Licensed under GPLv3 or later. see http://www.gnu.org/copyleft/gpl.html
 */

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Logger;

/**
 * User: John Lawrence
 * Date: 5/19/11
 * Time: 11:33 PM
 */
public class Client {
    private static Logger logger = Logger.getLogger(Client.class.getName());
    private static Socket socket = null;

    public static void main(String[] args) throws IOException {

        try {
            socket = new Socket("localhost", 25222);
            Scanner reader = new Scanner(socket.getInputStream());
            readLine(reader);
            writeLine("HELO userName");
            readLine(reader);
            logger.info("Quiting");
            writeLine("QUIT");
            readLine(reader);
            logger.info("Done !!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void writeLine(String message) throws IOException {
        String processedMessage = message + "\n";
        logger.info("Writing line: " + processedMessage);
        socket.getOutputStream().write(processedMessage.getBytes());
    }

    private static void readLine(Scanner reader) {
        String line = reader.nextLine();
        logger.info(line);
    }
}
